package flc.ast.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.TabAdapter;
import flc.ast.databinding.FragmentHotBinding;
import flc.ast.databinding.FragmentTabBinding;
import flc.ast.utils.Content;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatConstant;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResourceBean;

public class HotFragment extends BaseNoModelFragment<FragmentHotBinding> {

    private HotAdapter hotAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_hot;
    }

    @Override
    protected int getPageType() {
        return EventStatConstant.PAGE_OTHER;
    }

    public static HotFragment newInstance() {
        HotFragment fragment = new HotFragment();
        return fragment;
    }

    @Override
    protected void initView() {
        //tab
        mDataBinding.rvHotList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);
        hotAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getData();
        mDataBinding.refreshHotItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshHotItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshHotItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshHotItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshHotItem.finishRefresh(refreshTime);
            }
        });
    }

    private void getData() {
        StkResApi.getTagResourceList(null, AppConfig.HOT_DATA, StkApi.createParamMap(0, 10),false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                if (page == 1) {
                                    hotAdapter.setList(result);
                                } else {
                                    hotAdapter.addData(result);
                                }
                            }
                        }
                    }
                });
    }
}
